package com.sens.alipay;

import android.content.Context;

import com.sens.alipay.utils.SupportVersion;
import com.sens.bean.AlipayTrade;
import com.sens.common.ApplicationUtil;
import com.sens.common.Log;
import com.sens.database.GlobalSharedPreferences;
import com.sens.database.LiteOrmDBUtil;
import com.sens.enums.PreferencesSetting;
import com.sens.network.ApiByHttp;
import com.sens.network.ResponseCallback;
import com.sens.tables.AliPayConfig;
import com.sens.vo.AliPayOrderVo;
import com.yanzhenjie.nohttp.rest.Response;

import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

public class QuartzJob {
    //    private static String monitor;
    private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static void start(final Context context) {
        String versionName = ApplicationUtil.getVersionName(context);
        boolean supportVersion = false;
        for (String version : SupportVersion.VERSIONS) {
            if (version.equals(versionName)) {
                supportVersion = true;
                break;
            }
        }
        if (!supportVersion) {
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    long lastTime = GlobalSharedPreferences.getInstance(context).getLong(PreferencesSetting.QUARTZ_TIME);
                    long time = System.currentTimeMillis();
                    if (lastTime > 0 && time - lastTime < 10 * 1000) return;
                    GlobalSharedPreferences.getInstance(context).putLong(PreferencesSetting.QUARTZ_TIME, time);
                    String cookie = null;
                    AliPayConfig aliPayConfig = LiteOrmDBUtil.getGlobalDBUtil(context).queryOne(AliPayConfig.class);
                    if (aliPayConfig != null) {
                        cookie = aliPayConfig.getAlipayCookie();
                    }
                    if (cookie != null)
                        ApiByHttp.getInstance().queryAlipayOrder(20, 1, cookie, new ResponseCallback<AlipayTrade>() {
                            @Override
                            public void onSucceed(int what, Response<AlipayTrade> response) {
                                Log.d("时钟任务");
                                super.onSucceed(what, response);
                                if (response.get() != null && response.get().getResult() != null && response.get().getResult().getList() != null) {
                                    for (AlipayTrade.Data.Order order : response.get().getResult().getList()) {
                                        AliPayOrderVo aliPayOrderVo = new AliPayOrderVo();
                                        aliPayOrderVo.setTradeNo(order.getTradeNo());
                                        aliPayOrderVo.setMoney(order.getTotalPayedAmount());
                                        aliPayOrderVo.setMark((aliPayOrderVo.getMark() == null ? "" : aliPayOrderVo.getMark()) + "-来自" + order.getConsumerName());
                                        aliPayOrderVo.setTime(simpleDateFormat.format(order.getGmtCreateStamp()));

//                                        aliPayOrderVo.setMarkType(monitor);
//                                        aliPayOrderVo.setUnit("元");
//                                        aliPayOrderVo.setUserId();
//                                        AlipayOrder alipayOrder = LiteOrmDBUtil.getGlobalDBUtil(context).queryOneByWhere(AlipayOrder.class, "tradeNo", aliPayOrderVo.getTradeNo());
//                                        if (alipayOrder != null) continue;
//                                        alipayOrder = OrderUtil.createAlipayOrder(aliPayOrderVo);
//                                        LiteOrmDBUtil.getGlobalDBUtil(context).save(alipayOrder);
//                                        ApiByHttp.getInstance().uploadMybankOrder(alipayOrder, new MybankOrderCallback(context, alipayOrder.getTradeNo()));
                                    }
                                }
                            }
                        });
                }
            };
            new Timer().schedule(timerTask, 0, 10 * 1000);
        }
    }
}